package com.example.myapplication;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button query, insert, update, delete;
    EditText editText;
    ContentResolver resolver;//内容接收者

    private String baseUri="content://com.example.myapplication/";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化内容接收者
        resolver = getContentResolver();
        editText = findViewById(R.id.edittext);
        query = findViewById(R.id.query);
        insert = findViewById(R.id.insert);
        update = findViewById(R.id.update);
        delete = findViewById(R.id.delete);

        query.setOnClickListener(this);
        insert.setOnClickListener(this);
        update.setOnClickListener(this);
        delete.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        String text=editText.getText().toString().trim();
        switch (v.getId()) {
            case R.id.query:
                String strUri = baseUri+"select_"+text;
                //将string解析成uri对象
                Uri uri= Uri.parse(strUri);
                Log.d("cccc", "onClick: "+strUri);
                Cursor cursor = resolver.query(uri, null, null, null, null);
                    while (cursor.moveToNext()) {
                        int id = cursor.getInt(0);
                        String bookname = cursor.getString(1);
                        String author = cursor.getString(2);
                        Log.d("ccccc:  ", id + "---" + bookname + "----" + author);
                    }

                break;
            case R.id.insert:
                ContentValues values=new ContentValues();
                if ("book".equals(text)){
             String strUri2  = baseUri+"insert_"+text;
             Uri uri2=Uri.parse(strUri2);
                values.put("bookname","第一行代码");
                values.put("author","王二");
                resolver.insert(uri2,values);
                values.clear();
                }
                else  if ("user".equals(text)){
                    String strUri3  = baseUri+"insert_"+text;
                    Uri uri3=Uri.parse(strUri3);
                    values.put("username","张三");
                    values.put("phone","12345678901");
                    values.put("age",20);
                    resolver.insert(uri3,values);
                    values.clear();
                }
                break;
            case R.id.update:
                String strUri4=baseUri+"upate_"+text;
                Uri uri4=Uri.parse(strUri4);
                ContentValues values2=new ContentValues();
                if ("book".equals(text)){
                    values2.put("author","李四");
                    resolver.update(uri4,values2,"bookname=?",new String[]{"水浒传"});
                    values2.clear();
                }
                else if ("user".equals(text)){
                    values2.put("phone",1111111111);
                    resolver.update(uri4,values2,"age=?and username=?",new String[]{"18","TOM"});
                }
                break;
            case R.id.delete:
                String strUri5=baseUri+"delect_"+text;
                Uri uri5=Uri.parse(strUri5);
                if ("book".equals(text)){
                    resolver.delete(uri5,"bookname=?",new String[]{"红楼梦"});

                }else if ("user".equals(text)){
                    resolver.delete(uri5,"username=?",new String[]{"贾宝玉"});
                }
                break;
        }
    }
}
